﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;
using System.Text.RegularExpressions;


namespace CSCV.Group8.BlogRollWebPart.Helper
{
    /// <summary>
    /// Helper Library that contain the number of method using in Blog Roll Web Part
    /// </summary>
    public static class HelperLib
    {
        /// <summary>
        /// Check sitename is exist and is a blog site
        /// return false if not.
        /// </summary>
        /// <param name="siteName">URL of site</param>
        /// <returns></returns>
        public static bool CheckSPSiteisABlog(string siteName)
        {
            try
            {
                using (SPSite site = new SPSite(siteName))
                {
                    using (SPWeb web = site.OpenWeb())
                    {

                        if (!web.Exists)
                        {
                            return false;
                        }
                        else
                        {
                            if (web.WebTemplate == HelperConst.BLOG_TEMPLATE_STRING_TAG) return true;
                            return false;
                            //if (web.Lists.TryGetList(HelperConst.Comments) != null && web.Lists.TryGetList(HelperConst.PostList) != null)
                            //    return true;
                            //return false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// Check if list exist or not in SPweb
        /// </summary>
        /// <param name="web">the request spWeb</param>
        /// <param name="listName">the list need to check</param>
        /// <returns>True: exist / False : not exist</returns>
        public static bool ListExists(SPWeb web, string listName)
        {
            try
            {
                SPList list = web.Lists[listName];
            }
            catch
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// Slit string in to List by specific character
        /// </summary>
        /// <param name="source">The source of string to be slit</param>
        /// <param name="ch">The specific character to be slit by</param>
        /// <returns>The list of slited strings</returns>
        public static List<string> SplitStringToList(string source, params char[] ch)
        {
            string[] array = source.Split(ch);
            List<string> mysplit = new List<string>();
            foreach (string sub in array)
            {
                mysplit.Add(sub);
            }
            return mysplit;
        }

        /// <summary>
        /// Get SPUser by item and key
        /// </summary>
        /// <param name="item"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static SPUser GetSPUser(SPListItem item, string key)
        {
            SPFieldUser field = item.Fields.GetField(key) as SPFieldUser;

            if (field != null)
            {
                if (item[key] != null)
                {
                    SPFieldUserValue fieldValue = field.GetFieldValue(item[key].ToString()) as SPFieldUserValue;
                    if (fieldValue != null)
                    {
                        return fieldValue.User;
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// Get User Profile picture url of the username
        /// </summary>
        /// <param name="username">The username to be get profile picture</param>
        /// <returns>The user profile picture url</returns>
        public static string GetUserProfilePictureURL(string username)
        {
            string Url = string.Empty;
            try
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(SPContext.Current.Site);
                UserProfileManager upm = new UserProfileManager(serviceContext);
                if (upm.UserExists(username))
                {
                    UserProfile profile = upm.GetUserProfile(username);
                    if (profile != null)
                    {
                        if (profile[HelperConst.PICTURE_URL_TAG].Value != null)
                            Url = profile[HelperConst.PICTURE_URL_TAG].Value.ToString();
                    }
                }
                //change it to the default if the user does not have an image specified
                if (string.IsNullOrEmpty(Url))
                    Url = HelperConst.DEFAULT_PICTURE_URL_PATH_TAG;
            }
            catch (Exception ex)
            {
                return HelperConst.DEFAULT_PICTURE_URL_PATH_TAG;
            }
            return Url;
        }

        /// <summary>
        /// Compiled regular expression for performance. Remove the html tags in string
        /// </summary>
        /// <param name="inputString">string to be remove html tags</param>
        /// <returns></returns>
        public static string StripHTML(string inputString)
        {
            string output = Regex.Replace(inputString, HelperConst.HTML_PATTERN_TAG, string.Empty);
            output = output.Replace("\t", string.Empty);
            output = output.Replace("\r", string.Empty);
            output = output.Replace("\n", string.Empty);
            return output;
        }
        /// <summary>
        /// Get URL of Post
        /// </summary>
        /// <param name="path">Web URL string</param>
        /// <param name="PostID">Post Id</param>
        /// <returns></returns>
        public static string GetPostPath(string path, string PostID)
        {
            return string.Format("{0}/Lists/Posts/Post.aspx?ID={1}", path, PostID);
        }

        public static string ReplaceNewLine(string source)
        {
            return Regex.Replace(source, @"\r\n?|\n", HelperConst.NEWLINE_TAG);
        }
    }

}
